module.exports = class extends think.Model {
  // eslint-disable-next-line camelcase
  async list({ name, pageSize = 10, pageNum = 1, order = "id desc" }) {
    const where = {};
    if (name) {
      where.name = ["like", `%${name}%`];
    }

    const data = await this.where(where)
      .order(order)
      .page(pageNum, pageSize)
      .countSelect();
    return data;
  }

  async save({ id, ...form }) {
    if (id) {
      // eslint-disable-next-line no-return-await
      return await this.where({ id }).update({
        ...form,
        etime: ["exp", "now()"]
      });
    } else {
      // eslint-disable-next-line no-return-await
      return await this.add({
        ...form,
        ctime: ["exp", "now()"],
        etime: ["exp", "now()"]
      });
    }
  }

  async detail({ id }) {
    const data = await this.find(id);
    const contents = await this.model("survey_content")
      .where({ survey_id: id })
      .select();
    return { ...data, contents };
  }
};
